﻿// Copyright © Microsoft Corporation.  All Rights Reserved.
// This code released under the terms of the 
// Microsoft Public License (MS-PL, http://opensource.org/licenses/ms-pl.html.)

namespace Microsoft.Expression.Prototyping.Mockups
{
	using System;
	using System.Windows;
	using System.Windows.Controls;
	using System.Windows.Media;

	public enum IconPlacement
	{
		NoIcon,
		Top,
		Right,
		Bottom,
		Left,
		Center,
		Fill
	}

	public enum OriginLocation
	{
		TopLeft,
		Top,
		TopRight,
		Right,
		BottomRight,
		Bottom,
		BottomLeft,
		Left,
		Center
	}
	
	public enum IconImage
	{
		Add,
		ArrowAll,
		ArrowLeft,
		ArrowLeftRight,
		ArrowLeftTopRightBottom,
		ArrowRight,
		ArrowRightTopLeftBottom,
		ArrowUpDown,
		Ball,
		Bold,
		Click,
		CloseFile,
		Copy,
		Crosshair,
		Cursor,
		Cut,
		Delete,
		Document,
		FingerPointer,
		Folder,
		FontGrow,
		FontShrink,
		Go,
		Home,
		Hourglass,
		IBeam,
		Invalid,
		Italic,
		Locked,
		MouseLeftDown,
		MouseMiddleDown,
		MouseNoneDown,
		MouseRightDown,
		Music,
		NewFile,
		Next,
		None,
		OpenFile,
		Pan,
		Paragraph,
		Paste,
		Pause,
		Pencil,
		PersonAvatar,
		Pictures,
		Play,
		Plus,
		Previous,
		QuestionMark,
		Refresh,
		Repeat,
		RSS,
		SaveFile,
		Share,
		ShoppingCartEmpty,
		Star,
		Stop,
		Strikethrough,
		Underline,
		Videos,
		Warning,
		WindowClose,
		WindowFullScreen,
		WindowMaximize,
		WindowMinimize,
		Zoom
	}

	public class IconMockupBase : Control
	{
		public IconMockupBase()
		{
		}

		public OriginLocation OriginLocation
		{
			get { return (OriginLocation)GetValue(OriginLocationProperty); }
			set { SetValue(OriginLocationProperty, value); }
		}

		public static readonly DependencyProperty OriginLocationProperty = DependencyProperty.Register("OriginLocation", typeof(OriginLocation), typeof(IconMockupBase), new PropertyMetadata(OriginLocation.Center, null));

		// Position the center of the cursor overlay icon with respect to the cursor.  The extent of the cursor is (-0.5, 0.5), and the overlay can be centered outside of that range.
		internal Point OverlayLocation
		{
			get { return (Point)GetValue(OverlayLocationProperty); }
			set { SetValue(OverlayLocationProperty, value); }
		}

		public static readonly DependencyProperty OverlayLocationProperty = DependencyProperty.Register("OverlayLocation", typeof(Point), typeof(IconMockupBase), new PropertyMetadata(new Point(0.5, 0.25), null));
	}

	public class IconMockup : IconMockupBase
	{
		public IconMockup()
		{
			base.DefaultStyleKey = typeof(IconMockup);
			this.Loaded += (s, e) => this.ApplyIconChange();
		}

		public static readonly DependencyProperty IconImageProperty = DependencyProperty.Register("IconImage", typeof(IconImage), typeof(IconMockup), new PropertyMetadata(IconImage.Star, new PropertyChangedCallback(IconImageChanged)));

		public IconImage IconImage
		{
			get { return (IconImage)this.GetValue(IconImageProperty); }
			set { this.SetValue(IconImageProperty, value); }
		}

		private static void IconImageChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
		{
			((IconMockup)sender).ApplyIconChange();
		}

		protected virtual void ApplyIconChange()
		{
			this.ApplyTemplate();
			IconMockupBase chrome = this.FindObject(this, "IconMockupChrome") as IconMockupBase;

			if (chrome != null)
			{
				Style style = IconMockup.GetIconImageResource(this.IconImage) as Style;

				if (style != null)
				{
					chrome.Style = style;
				}

				this.OriginLocation = chrome.OriginLocation;
				this.OverlayLocation = chrome.OverlayLocation;
			}
		}

		private static IconImageResources resources;

		private static object GetIconImageResource(IconImage iconImage)
		{
			if (null == resources)
			{
				resources = new IconImageResources();
			}

			return resources["Icon_" + iconImage.ToString()];
		}

		private object FindObject(DependencyObject o, string name)
		{
			for (int i = 0; i < VisualTreeHelper.GetChildrenCount(o); i++)
			{
				DependencyObject child = VisualTreeHelper.GetChild(o, i);

				if (name.Equals(child.GetValue(FrameworkElement.NameProperty)))
				{
					return child;
				}

				object result = FindObject(child, name);

				if (result != null)
				{
					return result;
				}
			}

			return null;
		}
	}
}